<?php

namespace App\Http\Controllers\Estate;

use App\Http\Controllers\Controller;
use App\Models\ChargingScheme;
use Illuminate\Http\Request;

class ChargingSchemeController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $data=$request->query();
        if (empty($data['search'])){
            $query = ChargingScheme::query()
                ->orderBy('id', 'desc')
                ->paginate($request->per_page ?? 30);
            return $this->success($query);

        }else{
            $where=[];
            if($data['plan_title'])
            {
                $where[] = ['plan_title','like',"%{$data['plan_title']}%"];
            }
            if($data['fee_type'])
            {
                $where['fee_type'] = $data['fee_type'];
            }
            if($data['change'] != '')
            {
                $query = ChargingScheme::query()
                    ->whereDate('create_time', '>=',
                        date("Y-m-d H:i:s",strtotime($data['change'][0])))
                    ->whereDate('create_time', '<=',
                        date("Y-m-d H:i:s",strtotime($data['change'][1])))
                    ->where($where)
                    ->orderBy('id', 'desc')
                    ->paginate($request->per_page ?? 30);
            }else{
                $query = ChargingScheme::query()
                    ->where($where)
                    ->orderBy('id', 'desc')
                    ->paginate($request->per_page ?? 30);

            }
            return $this->success($query);
        }
    }
    public function EditChargingScheme($id = null,ChargingScheme $ChargingScheme)
    {
        if ($post=$_POST){

            if (isset($post['id']))
            {
                $query = $ChargingScheme
                    ->where('id' , $post['id'])
                    ->update($post);
            }else{
//                dd($post);
                $query = $ChargingScheme->insert($post);
            }

            return $this->success($query);

        }else{
            $query = $ChargingScheme::query()
                ->where('id' , $id)
                ->first();
            return $this->success($query);
        }

    }

    public function editChargingSchemeStatus(ChargingScheme $ChargingScheme)
    {

        $data=$_POST;
        if ($data){;
            $info = $ChargingScheme
                ->where('id', $data['id'])
                ->first();
            if ($info[$data['type']] == 0)
            {
                $query = $ChargingScheme
                    ->where('id' , $data['id'])
                    ->update([$data['type']=>1]);

            }else if ($info[$data['type']] == 1){

                $query = $ChargingScheme
                    ->where('id' , $data['id'])
                    ->update([$data['type']=>0]);
            }
            return $this->success($query);
        }
        return $this->error();

    }
    public function editChargingSchemeDel($id)
    {
        $ids=explode(',',$id);
        $query = ChargingScheme::query()
            ->whereIn('id' , $ids)
            ->delete();
        return $this->success($query);

    }

}
